package cn.cena.service.impl;

import cn.cena.constant.BaseConstant;
import cn.cena.doc.CourseDoc;
import cn.cena.domain.Course;
import cn.cena.domain.CourseDetail;
import cn.cena.domain.CourseMarket;
import cn.cena.domain.CourseType;
import cn.cena.dto.CourseDto;
import cn.cena.feign.client.SearchFeignClient;
import cn.cena.mapper.CourseMapper;
import cn.cena.service.ICourseDetailService;
import cn.cena.service.ICourseMarketService;
import cn.cena.service.ICourseService;
import cn.cena.service.ICourseTypeService;
import cn.cena.util.AjaxResult;
import cn.cena.util.AssertUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yu
 * @since 2021-11-26
 */
@Service
@Transactional
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private ICourseDetailService courseDetailService;

    @Autowired
    private ICourseMarketService courseMarketService;

    @Autowired
    private ICourseTypeService courseTypeService;

    @Autowired
    private SearchFeignClient searchFeignClient;


    @Override
    public void save(CourseDto dto) {
        Course course = dto.getCourse();
        CourseDetail courseDetail = dto.getCourseDetail();
        CourseMarket courseMarket = dto.getCourseMarket();
        // 参数判断，已在domain注解中JSR303
        // 判断是否重复
        EntityWrapper wrapper = new EntityWrapper();
        wrapper.eq("name", course.getName());
        Course one = super.selectOne(wrapper);
        AssertUtil.isNotNull(one, "该课程已存在");

        // 保存课程表
        // 机构和user TODO
        course.setTenantId(26L);
        course.setTenantName("源码时代");
        course.setUserId(null);
        course.setUserName("");
        course.setCommentCount(0);
        course.setStatus(BaseConstant.Course.COURSE_OFFLIINE);
        course.setSaleCount(0);
        course.setViewCount(0);
        super.insert(course);

        // 获得课程返回的id
        Long id = course.getId();
        // 保存课程详情表
        courseDetail.setId(id);
        courseDetailService.insert(courseDetail);

        // 保存课程营销表
        courseMarket.setId(id);
        courseMarketService.insert(courseMarket);

        // 课程类型表totalCount修改
        CourseType courseType = courseTypeService.selectById(course.getCourseTypeId());
        String[] courseTypeIds = courseType.getPath().split("\\.");
        courseTypeService.updateTotalCountById(1,courseTypeIds);

    }

    /**
     * 课程上线
     * @param id
     */
    @Override
    public void onLineCourse(Long id) {

        // 判断课程是否存在
        Course course = super.selectById(id);
        AssertUtil.isNull(course, "课程不存在");

        // 判断课程状态
        AssertUtil.isFalse(course.getStatus() != BaseConstant.Course.COURSE_OFFLIINE, "课程已上线");

        // 修改课程状态
        course.setStatus(BaseConstant.Course.ONLINE);
        course.setOnlineTime(new Date());
        super.updateById(course);

        // 存入ElasticSearch
        CourseDoc courseDoc = new CourseDoc();
        CourseMarket courseMarket = courseMarketService.selectById(id);
        courseDoc.setChargeName(courseMarket.getCharge() == 1?"免费" : "收费");
        BeanUtils.copyProperties(course, courseDoc);
        BeanUtils.copyProperties(courseMarket, courseDoc);

        AjaxResult result = searchFeignClient.onlineCourse(courseDoc);


    }
}
